Skip to content

fix(clangd): attach document version to publishDiagnostics - #10

Merged
devnullexile merged 1 commit into
mainfrom
fix/issue-5-publish-diagnostics-version
Oct 14, 2026
Merged

devnullexile merged 1 commit into
mainfrom
fix/issue-5-publish-diagnostics-version

Conversation

@devnullexile

@devnullexile devnullexile commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

What

publishDiagnosticsFor attached the document version only when llvm::to_integer failed (inverted check). Numeric versions were never sent, and a non-numeric one would have arrived as version: 0. Without the version, clients can't drop diagnostics computed for an older text state, so a slow parse of an earlier edit can overwrite newer diagnostics while typing.

Changes

  • Pass the version through as std::optional<int64_t> instead of round-tripping it through a string. This removes the inverted check.
  • When the client omits a version, publish the one DraftStore assigns, as ClangdLSPServer does. updateDraft now takes and returns the integer version, so the module has one version representation.
  • Clear published diagnostics when an incremental change fails to apply and the draft is dropped, tagged with the rejected edit's version. Previously stale diagnostics stayed in the editor until the file was reopened. Clearing (here and on didClose) uses the canonical file URI, as publishing does.
  • Honor clangd's wantDiagnostics: false on didChange. forceRebuild needs no handling because every publish is a fresh parse.
  • README: note the version and wantDiagnostics behavior.

Tests

  • New PseudoModuleLSPTest fixture in PseudoModuleTests. It records outgoing notifications and covers explicit versions plus didClose, synthesized versions, failed changes clearing diagnostics, and wantDiagnostics: false.
  • test_lsp.py asserts that the syntax-error diagnostics are published for version 1 on didOpen, for version 2 after a didChange, that wantDiagnostics: false skips publishing, and that an omitted version is synthesized.

Verification

  • Locally (macOS, Homebrew LLVM 23.1.0): PseudoModuleTests 76/76, ClangPseudoTests 46/46, check-clang-pseudo 152/152.
  • I ran test_lsp.py against the clangd from the latest main CI artifact (llvm aabe42e):
    • With the unfixed plugin it fails with Expected syntax error diagnostics published for version 1, got [None].
    • With this branch's plugin it passes.
  • With the old inverted check temporarily put back, the new unit test fails.

Fixes #5

@devnullexile
devnullexile requested a review from ArcsinX as a code owner October 13, 2026 09:02

@ArcsinX ArcsinX left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR! Here is a detailed review. Overall, fixing document version propagation on publishDiagnostics is a great improvement. However, there are several issues and edge cases around URI canonicalization, version preservation on failed edits, API backward compatibility, and test assertions that should be addressed.

Comment thread clangd/PseudoModule.h Resolved
Comment thread clangd/PseudoModule.h Resolved
Comment thread clangd/PseudoModule.h Resolved
Comment thread clangd/PseudoModule.cpp Resolved
Comment thread clangd/PseudoModule.cpp Resolved
Comment thread clangd/PseudoModule.cpp Resolved
Comment thread unittests/clangd/PseudoModuleTests.cpp Resolved
Comment thread unittests/clangd/PseudoModuleTests.cpp Resolved
Comment thread unittests/clangd/PseudoModuleTests.cpp Resolved
Comment thread test_lsp.py Resolved
@devnullexile
devnullexile force-pushed the fix/issue-5-publish-diagnostics-version branch from 6de8f40 to 1e567b5 Compare October 13, 2026 10:44

@DrSergei DrSergei left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Low — clangd/PseudoModule.cpp:245-251: failed didChange still calls clearDiagnosticsFor(...) even when wantDiagnostics=false, contradicting the commit's goal of honoring the flag. Edge case (unapplanable edit + flag off); arguably safe since the draft is gone.
  2. Informational — clangd/PseudoModule.cpp:183-197: the non-numeric elog/nullopt branch in decodeVersion is unreachable via updateDraft (mirrors upstream defensive code). No change needed.
  3. Test note — test_lsp.py:130-132: syntax_error_versions1 only captures the publish it breaks on, so it doesn't verify all syntax-error publishes carry version 1. Not a correctness bug.

Overall, LGTM

publishDiagnosticsFor set PublishDiagnosticsParams::version only when
llvm::to_integer failed, so numeric versions were dropped and
non-numeric strings were sent as 0. Clients need the version to discard
diagnostics computed for an older text state.

Pass the version DraftStore assigned to the draft straight through as
std::optional<int64_t>, which removes the inverted check. Like
ClangdLSPServer, publish the synthesized draft version when the client
omits one. updateDraft now takes and returns the integer version, so
the module has a single version representation.

Also clear published diagnostics when an incremental change fails to
apply and the draft is dropped, tagged with the rejected edit's
version, and honor wantDiagnostics=false on didChange. The clear
ignores wantDiagnostics, since without a draft nothing would replace
those diagnostics. Publishing and clearing share one notification
builder that uses the canonical file URI.

Validation: PseudoModuleTests (77), ClangPseudoTests (46) and
check-clang-pseudo (153) pass; test_lsp.py passes against the CI
clangd build and fails with the unfixed plugin.

Fixes #5
@devnullexile
devnullexile force-pushed the fix/issue-5-publish-diagnostics-version branch from 1e567b5 to 1d0e884 Compare October 13, 2026 11:06
@devnullexile

Copy link
Copy Markdown
Member Author

@DrSergei, on your three notes (addressed in 1d0e884):

  1. Clear despite wantDiagnostics=false. I kept the clear on purpose. When an edit fails to apply, the draft is dropped, and every later didChange for the file returns early at "Trying to incrementally change non-added document". If the clear were skipped because of the flag, nothing would ever replace those diagnostics, so they'd stay in the editor until the file is reopened. That is the case the clear was added for. The code now says so in a comment, and FailedChangeClearsDiagnosticsDespiteWantDiagnostics pins the behavior. I checked that this test fails if the clear is gated on the flag.
  2. Unreachable non-numeric branch in decodeVersion. Agreed. It's kept as defensive code that mirrors upstream, with no change.
  3. test_lsp.py version capture. The list is now a single syntax_error_version1 taken from the publish that carries the syntax error, since only one is collected. The test1 and test2 waits now share the next_publish helper.

Also in this push, with no behavior change: publishing and clearing go through one private sendDiagnostics (null check, canonical URI, version), and the failed-edit and close paths call removeDraft.

Local checks: PseudoModuleTests 77/77, ClangPseudoTests 46/46, check-clang-pseudo 153/153, and test_lsp.py against the CI clangd.

@ArcsinX ArcsinX left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! All comments have been cleanly addressed:

  • Restored default Version = std::nullopt across updateDraft and publishDiagnosticsFor.
  • Consolidated diagnostics dispatch and URI canonicalization via sendDiagnostics, ensuring consistent canonical URIs for both publishDiagnosticsFor and clearDiagnosticsFor.
  • Tagged cleared diagnostics on failed edits with the rejected edit's version.
  • Resolved object lifetime order in PseudoModuleLSPTest and returned copied JSON objects in published().
  • Extended test_lsp.py with end-to-end didChange version verification and wantDiagnostics: false suppression checks.

All tests (77/77 PseudoModuleTests and test_lsp.py) pass.

@devnullexile
devnullexile merged commit bcb3aed into main Oct 14, 2026
4 checks passed
@devnullexile
devnullexile deleted the fix/issue-5-publish-diagnostics-version branch October 14, 2026 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

publishDiagnostics never carries the document version (inverted to_integer check)

4 participants